package com.yequan.hbb.customview;

import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Chronometer;



@SuppressLint(
{ "ViewConstructor", "SimpleDateFormat" })
public class DateCount extends Chronometer
{
	public DateCount(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		mTimeFormat = new SimpleDateFormat("d天 H:mm:ss");
		this.setOnChronometerTickListener(listener);
	}

	private long mTime;
	private long mNextTime;
	private OnTimeCompleteListener mListener;
	private SimpleDateFormat mTimeFormat;

	public DateCount(Context context)
	{
		super(context);

	}

	/**
	 * ����������ʱ
	 */
	public void reStart(long _time_s)
	{
		if (_time_s == -1)
		{
			mNextTime = mTime;
		} else
		{
			mTime = mNextTime = _time_s;
		}
		this.start();
	}

	public void reStart()
	{
		reStart(-1);
	}

	public void onResume()
	{
		this.start();
	}

	public void onPause()
	{
		this.stop();
	}

	public void setTimeFormat(String pattern)
	{
		mTimeFormat = new SimpleDateFormat(pattern);
	}

	public void setOnTimeCompleteListener(OnTimeCompleteListener l)
	{
		mListener = l;
	}

	OnChronometerTickListener listener = new OnChronometerTickListener()
	{
		@Override
		public void onChronometerTick(Chronometer chronometer)
		{
			if (mNextTime <= 0)
			{
				if (mNextTime == 0)
				{
					DateCount.this.stop();
					if (null != mListener)
						mListener.onTimeComplete();
				}
				mNextTime = 0;
				updateTimeText();
				return;
			}

			mNextTime=mNextTime-1000;

			updateTimeText();
		}
	};

	public void initTime(long _time_s)
	{
		mTime = mNextTime = _time_s;
		updateTimeText();
	}
	
	private void updateTimeText()
	{



		this.setText(mTimeFormat.format(mNextTime));
	}

    public 	static interface OnTimeCompleteListener
	{
		void onTimeComplete();
	}

}
